package com.ds.context;

import com.ds.esb.util.EsbFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SpringMvcContextImpl extends JDSActionContext {
	public Map sessionMap = new HashMap();

	public Map contextMap = new HashMap();

	public SpringMvcContextImpl() {

	}

	public Object Par(String expression) {
		Object obj = null;
		try {
			obj = Par(expression, null);
		} catch (Exception e) {

		}
		return obj;
	}

	public Map getContext() {

		if (RequestContextHolder.getRequestAttributes() != null) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			this.contextMap.putAll(request.getParameterMap());
		}

		return this.contextMap;
	}

	public Object getParams(String param) {
		String value = null;

		if (RequestContextHolder.getRequestAttributes() != null) {

			ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			if (requestAttributes != null) {
				HttpServletRequest request = requestAttributes.getRequest();
				if (request != null) {
					value = (String) request.getAttribute(param);

					if ((value == null || value.equals("")) && (request.getSession() != null) && (request.getSession().getAttribute(param) != null)) {

						value = request.getSession().getAttribute(param).toString();
					}

					if (value == null || value.equals("")) {
						Object obj = getContext().get(param);
						if (obj != null) {
							if (obj.getClass().isArray()) {
								Object[] objs = (Object[]) getContext().get(param);
								if (objs.length > 0 && objs[0] != null) {
									value = objs[0].toString();
								}

							} else {
								value = obj.toString();
							}
						}

					}
					if (value == null) {
						value = request.getParameter(param);
					}

				}
			}

		}

		return value;
	}

	public String getIpAddr() {
		String ip = null;
		if (RequestContextHolder.getRequestAttributes() != null) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			ip = request.getHeader("X-Forwarded-For");

			String gatewayip = request.getHeader("gateway-ip");
			if ((gatewayip != null) && (!gatewayip.equals(""))) {
				ip = gatewayip;
			}

			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("HTTP_CLIENT_IP");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
				ip = request.getRemoteAddr();
			}
		}
		return ip;
	}

	public Map getSession() {
		if (RequestContextHolder.getRequestAttributes() != null) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			if (request != null) {
				HttpSession session = request.getSession(true);
				this.sessionMap = ((Map) session.getAttribute("sessionMap"));
				if (this.sessionMap == null) {
					this.sessionMap = new HashMap();
					session.putValue("sessionMap", this.sessionMap);
				}
			}
		}

		return this.sessionMap;
	}

	public String getSessionId() {
		String sessionid = null;
		if (RequestContextHolder.getRequestAttributes() != null) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			sessionid = (String) getParams("JSESSIONID");

			String rSessionId = (String) getParams("RSESSIONID");
			if ((rSessionId != null) && (!rSessionId.equals(""))) {
				sessionid = rSessionId;
			}
			if ((sessionid == null) || (sessionid.equals("") || sessionid.equals("\"\""))) {
				// sessionid =request.getSession(true).getId();
				sessionid = UUID.randomUUID().toString().toUpperCase();
				this.getSession().put("JSESSIONID", sessionid);
				request.getSession().setAttribute("JSESSIONID", sessionid);
			}
		}
		//this.getContext().put("RSESSIONID", sessionid);
		this.getSession().put("JSESSIONID", sessionid);

		return sessionid;

	}

	public <T> T Par(String expression,Class<T> clazz) {
		return Par(expression, clazz, null);
	}

	public <T> T Par(String expression, Class<T> clazz, Object source) {

		T obj = EsbFactory.par(expression, this.contextMap,null,clazz);

		return obj;

	}

	public Object getHttpRequest() {
		HttpServletRequest request = null;
		if (RequestContextHolder.getRequestAttributes() != null) {
			request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		}

		return request;
	}

	public String getHost() {

		if (RequestContextHolder.getRequestAttributes() != null) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			return request.getHeader("host");
		}
		return null;

	}
}
